package com.java.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author jingjintao
 * @date 2021/6/16 - 23:52
 **/
public class ContextServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext context = getServletConfig().getServletContext();
        //1.获取上下文参数
        String username = context.getInitParameter("username");
        String password = context.getInitParameter("password");
        System.out.println("context-param参数username的值是:"+username);
        System.out.println("context-param参数password的值是:"+password);
        //2.获取当前的工程路径，格式:/工程路径
        System.out.println("当前工程路径:"+context.getContextPath());

        //获取当前工程部署后在服务器硬盘上的绝对路径
        /**
         * 斜杠被服务器解析地址为:http://ip:port/工程名/  映射到IDEA代码的web目录
         */
        System.out.println("工程部署的绝对路径是:"+context.getRealPath("/"));
        System.out.println("工程下css的绝对路径是:"+context.getRealPath("/css"));
        System.out.println("工程下images吗目录01.jpg的绝对路径是:"+context.getRealPath("/images/01.jpg"));
    }
}
